package net.alteiar.ui.algorithms;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class PanelAlgorithmEdgeDetector extends
		PanelAlgorithmsDetails<AlgorithmEdgeDetector> {
	private static final long serialVersionUID = 1L;

	private final JSpinner spinnerLowThreshold;
	private final JSpinner spinnerHighThreshold;
	private final JSpinner spinnerKernelRadius;
	private final JSpinner spinnerKernelWidth;
	private final JCheckBox checkBoxContrastNormalized;

	public PanelAlgorithmEdgeDetector() {
		super(new AlgorithmEdgeDetector());
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 68, 56, 29, 0 };
		gridBagLayout.rowHeights = new int[] { 30, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JLabel lblLowThreshold = new JLabel("low threshold:");
		GridBagConstraints gbc_lblLowThreshold = new GridBagConstraints();
		gbc_lblLowThreshold.anchor = GridBagConstraints.EAST;
		gbc_lblLowThreshold.insets = new Insets(0, 0, 5, 5);
		gbc_lblLowThreshold.gridx = 0;
		gbc_lblLowThreshold.gridy = 0;
		add(lblLowThreshold, gbc_lblLowThreshold);

		spinnerLowThreshold = new JSpinner();
		spinnerLowThreshold.setModel(new SpinnerNumberModel(2.5f, null, null,
				0.5f));
		GridBagConstraints gbc_spinnerLow;
		gbc_spinnerLow = new GridBagConstraints();
		gbc_spinnerLow.fill = GridBagConstraints.HORIZONTAL;
		gbc_spinnerLow.insets = new Insets(0, 0, 5, 5);
		gbc_spinnerLow.gridx = 1;
		gbc_spinnerLow.gridy = 0;
		add(spinnerLowThreshold, gbc_spinnerLow);

		JLabel lblHighThreshold = new JLabel("High threshold:");
		GridBagConstraints gbc_lblHighThreshold = new GridBagConstraints();
		gbc_lblHighThreshold.anchor = GridBagConstraints.EAST;
		gbc_lblHighThreshold.insets = new Insets(0, 0, 5, 5);
		gbc_lblHighThreshold.gridx = 0;
		gbc_lblHighThreshold.gridy = 1;
		add(lblHighThreshold, gbc_lblHighThreshold);

		spinnerHighThreshold = new JSpinner();
		spinnerHighThreshold.setModel(new SpinnerNumberModel(7.5f, null, null,
				0.5f));
		GridBagConstraints gbc_spinnerHighThreshold = new GridBagConstraints();
		gbc_spinnerHighThreshold.fill = GridBagConstraints.HORIZONTAL;
		gbc_spinnerHighThreshold.insets = new Insets(0, 0, 5, 5);
		gbc_spinnerHighThreshold.gridx = 1;
		gbc_spinnerHighThreshold.gridy = 1;
		add(spinnerHighThreshold, gbc_spinnerHighThreshold);

		JLabel lblGaussianKernel = new JLabel("Gaussian Kernel Radius:");
		GridBagConstraints gbc_lblGaussianKernel = new GridBagConstraints();
		gbc_lblGaussianKernel.anchor = GridBagConstraints.EAST;
		gbc_lblGaussianKernel.insets = new Insets(0, 0, 5, 5);
		gbc_lblGaussianKernel.gridx = 0;
		gbc_lblGaussianKernel.gridy = 2;
		add(lblGaussianKernel, gbc_lblGaussianKernel);

		spinnerKernelRadius = new JSpinner(new SpinnerNumberModel(2f, null,
				null, 1f));
		GridBagConstraints gbc_spinnerKernelRadius = new GridBagConstraints();
		gbc_spinnerKernelRadius.fill = GridBagConstraints.HORIZONTAL;
		gbc_spinnerKernelRadius.insets = new Insets(0, 0, 5, 5);
		gbc_spinnerKernelRadius.gridx = 1;
		gbc_spinnerKernelRadius.gridy = 2;
		add(spinnerKernelRadius, gbc_spinnerKernelRadius);

		JLabel lblGaussianKernelWidth = new JLabel("Gaussian Kernel Width:");
		GridBagConstraints gbc_lblGaussianKernelWidth = new GridBagConstraints();
		gbc_lblGaussianKernelWidth.anchor = GridBagConstraints.EAST;
		gbc_lblGaussianKernelWidth.insets = new Insets(0, 0, 5, 5);
		gbc_lblGaussianKernelWidth.gridx = 0;
		gbc_lblGaussianKernelWidth.gridy = 3;
		add(lblGaussianKernelWidth, gbc_lblGaussianKernelWidth);

		spinnerKernelWidth = new JSpinner(
				new SpinnerNumberModel(16, 1, null, 1));
		GridBagConstraints gbc_spinnerKernelWidth = new GridBagConstraints();
		gbc_spinnerKernelWidth.fill = GridBagConstraints.HORIZONTAL;
		gbc_spinnerKernelWidth.insets = new Insets(0, 0, 5, 5);
		gbc_spinnerKernelWidth.gridx = 1;
		gbc_spinnerKernelWidth.gridy = 3;
		add(spinnerKernelWidth, gbc_spinnerKernelWidth);

		JLabel lblContrastNormalized = new JLabel("Contrast Normalized:");
		GridBagConstraints gbc_lblContrastNormalized = new GridBagConstraints();
		gbc_lblContrastNormalized.anchor = GridBagConstraints.EAST;
		gbc_lblContrastNormalized.insets = new Insets(0, 0, 0, 5);
		gbc_lblContrastNormalized.gridx = 0;
		gbc_lblContrastNormalized.gridy = 4;
		add(lblContrastNormalized, gbc_lblContrastNormalized);

		checkBoxContrastNormalized = new JCheckBox("");
		GridBagConstraints gbc_checkBoxContrastNormalized = new GridBagConstraints();
		gbc_checkBoxContrastNormalized.insets = new Insets(0, 0, 0, 5);
		gbc_checkBoxContrastNormalized.gridx = 1;
		gbc_checkBoxContrastNormalized.gridy = 4;
		add(checkBoxContrastNormalized, gbc_checkBoxContrastNormalized);
	}

	@Override
	protected void updateParams() {
		AlgorithmEdgeDetector algo = getAlgorithm();

		algo.setLowThreshold((float) spinnerLowThreshold.getValue());
		algo.setHighThreshold((float) spinnerHighThreshold.getValue());

		algo.setGaussianKernelRadius((float) spinnerKernelRadius.getValue());
		algo.setGaussianKernelWidth((int) spinnerKernelWidth.getValue());

		algo.setContrastNormalized(checkBoxContrastNormalized.isSelected());
	}

}
